package com.igeekhome.controller;

import com.igeekhome.dto.WaybillDto;
import com.igeekhome.pojo.*;
import com.igeekhome.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Romeo
 */
@Controller
@RequestMapping("/page/waybill")
public class WaybillController {
    @Autowired
    private IDestService destService;
    @Autowired
    private IStaffService sysStaffService;

    @Autowired
    private IDiceService diceService;

    @Autowired
    private IExpressCompanyService expressCompanyService;

    @Autowired
    private INetworkService networkService;
    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IQuoteDtoService quoteDtoService;

    @Autowired
    private IWaybillService waybillService;

    @Value("${PICKUP_CLERK}")
    private Integer PICKUP_CLERK;

    @Value("${SHIPMENT_TYPE}")
    private String SHIPMENT_TYPE;

    @Value("${TRANSPORT_METHOD}")
    private String TRANSPORT_METHOD;

    @Value("${PRODUCT_TYPE}")
    private String PRODUCT_TYPE;

    @Value("${PAYMENT_METHOD}")
    private String PAYMENT_METHOD;
    
    @RequestMapping("/list")
    public String list(Model model, HttpSession session){
        plugValue(model,session);
        return "pages/waybill/waybill";
    }
    @RequestMapping("/add")
    public String add(Model model, HttpSession session){
        plugValue(model,session);
        return "pages/waybill/waybill-add";
    }
    
    /**
     * @汪璇
     * @Description 运单编辑页面跳转
     **/
    @RequestMapping("/edit")
    public String edit(Model model, HttpSession session){
        plugValue(model,session);
        return "pages/waybill/waybill-edit";
    }

    @RequestMapping("/printing/{waybillId}")
    public String printing( Model model, @PathVariable("waybillId") Integer waybillId,HttpSession session){
        DNetwork network = (DNetwork) session.getAttribute("network");
        Integer networkId = network.getNetworkId();
        WaybillDto waybill = waybillService.selectByWid(waybillId,networkId);
        model.addAttribute("waybill",waybill);
        return "pages/waybill/print";

    }
    
    /**
     * @Description 往作用域里塞值
     * @Param [model, session]
     * @Return void
     **/
    private void plugValue(Model model, HttpSession session){
        List<DSpotDestination> destinations = destService.toShow();
        
        List<DSendingMaintenanceList> spots = quoteDtoService.selectToSpots();
        
        DNetwork network = (DNetwork)session.getAttribute("network");
        List<SysStaff> pickups = sysStaffService.selectByIds(PICKUP_CLERK, network.getNetworkId());
        
        List<DSendingMaintenanceList> sending = quoteDtoService.selectByNid(network.getNetworkId());
        
        List<BaseDict> shifts = diceService.selectByTypeCode(SHIPMENT_TYPE);
        List<BaseDict> transports = diceService.selectByTypeCode(TRANSPORT_METHOD);
        
        List<DExpressCompany> companies = expressCompanyService.select();
        
        List<DNetwork> networks = networkService.selectAll();
        
        List<DCustomer> customers = customerService.selectToList();
        
        List<BaseDict> products = diceService.selectByTypeCode(PRODUCT_TYPE);
        
        List<BaseDict> payments = diceService.selectByTypeCode(PAYMENT_METHOD);
        
        model.addAttribute("spots",spots).addAttribute("destinations",destinations).addAttribute("sending",sending).addAttribute("pickups",pickups).addAttribute("shifts",shifts)
                .addAttribute("transports",transports).addAttribute("companies",companies).addAttribute("networks",networks)
                .addAttribute("customers",customers).addAttribute("products",products).addAttribute("payments",payments);
        
    }
    

}
